package cn.stylefeng.guns.modular.system.orgdata.controller;

import cn.stylefeng.guns.modular.system.orgdata.constants.OrgDataConstants;
import cn.stylefeng.guns.modular.system.orgdata.entity.OrgData;
import cn.stylefeng.guns.modular.system.orgdata.pojo.request.CommonOrgDataTreeRequest;
import cn.stylefeng.guns.modular.system.orgdata.pojo.request.OrgDataRequest;
import cn.stylefeng.guns.modular.system.orgdata.pojo.response.CommonOrgDataTreeResponse;
import cn.stylefeng.guns.modular.system.orgdata.service.OrgDataService;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.annotation.BizLog;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据机构信息控制器
 *
 * @author fengshuonan
 * @date 2023/06/10 21:23
 */
@RestController
@ApiResource(name = "数据机构信息")
public class OrgDataController {

    @Resource
    private OrgDataService orgdataService;

    /**
     * 添加数据机构
     *
     * @author fengshuonan
     * @date 2023/06/10 21:23
     */
    @PostResource(name = "添加数据机构", path = "/orgdata/add", requiredPermission = true,
            requirePermissionCode = OrgDataConstants.ADD_ORGDATA)
    @BizLog(logTypeCode = OrgDataConstants.ADD_ORGDATA)
    public ResponseData<OrgData> add(
            @RequestBody @Validated(OrgDataRequest.add.class) OrgDataRequest OrgdataRequest) {
        orgdataService.add(OrgdataRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 删除数据机构
     *
     * @author fengshuonan
     * @date 2023/06/10 21:23
     */
    @PostResource(name = "删除数据机构", path = "/orgdata/delete", requiredPermission = true,
            requirePermissionCode = OrgDataConstants.DELETE_ORGDATA)
    @BizLog(logTypeCode = OrgDataConstants.DELETE_ORGDATA)
    public ResponseData<?> delete(@RequestBody @Validated(OrgDataRequest.delete.class) OrgDataRequest OrgdataRequest) {
        orgdataService.del(OrgdataRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 批量删除数据机构
     *
     * @author fengshuonan
     * @date 2023/06/10 21:23
     */
    @PostResource(name = "批量删除数据机构", path = "/orgdata/batchDelete", requiredPermission = true,
            requirePermissionCode = OrgDataConstants.DELETE_ORGDATA)
    @BizLog(logTypeCode = OrgDataConstants.DELETE_ORGDATA)
    public ResponseData<?> batchDelete(@RequestBody @Validated(BaseRequest.batchDelete.class) OrgDataRequest OrgdataRequest) {
        orgdataService.batchDelete(OrgdataRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 编辑数据机构
     *
     * @author fengshuonan
     * @date 2023/06/10 21:23
     */
    @PostResource(name = "编辑数据机构", path = "/orgdata/edit", requiredPermission = true,
            requirePermissionCode = OrgDataConstants.EDIT_ORGDATA)
    @BizLog(logTypeCode = OrgDataConstants.EDIT_ORGDATA)
    public ResponseData<?> edit(@RequestBody @Validated(OrgDataRequest.edit.class) OrgDataRequest OrgdataRequest) {
        orgdataService.edit(OrgdataRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 查看数据机构详情
     *
     * @author fengshuonan
     * @date 2023/06/10 21:23
     */
    @GetResource(name = "查看数据机构详情", path = "/orgdata/detail")
    public ResponseData<OrgData> detail(@Validated(OrgDataRequest.detail.class) OrgDataRequest OrgdataRequest) {
        return new SuccessResponseData<>(orgdataService.detail(OrgdataRequest));
    }

    /**
     * 获取列表（带分页）
     *
     * @author fengshuonan
     * @date 2023/06/10 21:23
     */
    @GetResource(name = "分页查询-数据机构", path = "/orgdata/page")
    public ResponseData<PageResult<OrgData>> page(OrgDataRequest OrgdataRequest) {
        return new SuccessResponseData<>(orgdataService.findPage(OrgdataRequest));
    }


    /**
     * 通用获取组织机构树
     * <p>
     * ps：用在获取用户管理和组织机构管理界面左侧树
     * <p>
     * 如果传递了orgParentId，则为懒加载，传递-1，获取所有一级节点
     *
     * @author fengshuonan
     * @since 2023/6/11 10:31
     */
    @PostResource(name = "通用获取组织机构树", path = "/common/orgdata/tree")
    public ResponseData<CommonOrgDataTreeResponse> commonOrgTree(@RequestBody CommonOrgDataTreeRequest commonOrgdataTreeRequest) {
        return new SuccessResponseData<>(orgdataService.commonOrgdataTree(commonOrgdataTreeRequest));
    }

    /**
     * 分页获取组织机构信息（用在通用选择机构组件中）
     *
     * @author fengshuonan
     * @since 2023/6/29 9:24
     */
    @GetResource(name = "分页获取组织机构信息（用在通用选择机构组件中）", path = "/common/orgdata/pageList")
    public ResponseData<PageResult<OrgData>> commonOrdatagPage(OrgDataRequest orgdataRequest) {
        return new SuccessResponseData<>(orgdataService.commonOrgPage(orgdataRequest));
    }

    /**
     * 获取机构名称集合，通过机构id的列表
     *
     * @author fengshuonan
     * @since 2023/11/2 10:16
     */
    @PostResource(name = "获取机构名称集合，通过机构id的列表", path = "/common/orgdata/getOrgdataListName")
    public ResponseData<List<SimpleDict>> getOrgdataListName(
            @RequestBody @Validated(BaseRequest.batchDelete.class) OrgDataRequest orgdataRequest) {
        return new SuccessResponseData<>(orgdataService.getOrgListName(orgdataRequest));
    }
}

